﻿using HandyControl.Controls;
using IoTClient.Clients.PLC;
using IoTClient.Enums;
using NLog.LayoutRenderers;
using QModels;
using QServices;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace QControlCenter
{
    public class ModBusTcp
    {

        private string IP { get; set; }
        private int PORT { get; set; }

        private PipelinedTcpClient m_plc { get; set; }

        public ModBusTcp(string ip, int port)
        {
            IP = ip;
            PORT = port;
            m_plc = new PipelinedTcpClient(IP, PORT);
        }
        public PipelinedTcpClient GetPlc()
        {
            if (m_plc == null) m_plc = new PipelinedTcpClient(IP, PORT);
            try
            {
                //m_plc.Connect();
            }
            catch (Exception ex)
            {
                Growl.ErrorGlobal("打开PLC失败");
                AppRuntime.Logger_Error("打开PLC失败");
            }

            return m_plc;
        }

        public int 温度探头1()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("温度探头1");
            return res;
        }
        public int 温度探头2()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("温度探头2");
            return res;
        }
        public int 温度探头3()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("温度探头3");
            return res;
        }
        public (decimal temperature_in, decimal temperature_out, int OK) GetAvageTemperature()
        {


            int OK = 0;
            var temp_arr = new int[4];
            var temperature_out = 0.0m;
            var t_names = new string[] { "温度探头1", "温度探头2", "温度探头3", "室外温度探头" };
            var plc = GetPlc();
            for (int i = 0; i < t_names.Length; i++)
            {
                try
                {
                    var tt = plc.ReadInt32(t_names[i]);
                    temp_arr[i] = tt;
                    OK++;
                }
                catch (Exception ex)
                {
                    AppRuntime.Logger_Error(ex);
                }
            }
            temperature_out = temp_arr[t_names.Length - 1];
            var temperature_in = (decimal)temp_arr.Take(temp_arr.Length - 1).Average();
            return (temperature_in, temperature_out, OK);
        }


        public (decimal temperature_in, decimal temperature_out, decimal humidy, decimal co2, decimal o3) GetHomePageData()
        {
            int OK = 0;
            int humidy = 0;
            int co2 = 0;
            int o3 = 0;
            var temp_arr = new int[4];
            var temperature_out = 0.0m;
            var t_names = new string[] { "温度探头1", "温度探头2", "温度探头3", "室外温度探头", "二氧化碳探头" };
            var plc = GetPlc();
            for (int i = 0; i < t_names.Length; i++)
            {
                try
                {
                    var tt = plc.ReadInt32(t_names[i]);
                    temp_arr[i] = tt;
                    OK++;
                }
                catch (Exception ex)
                {
                    AppRuntime.Logger_Error(ex);
                }
            }
            temperature_out = temp_arr[t_names.Length - 2];
            co2 = temp_arr[t_names.Length - 1];
            var temperature_in = (decimal)temp_arr.Take(temp_arr.Length - 1).Average();
            return (temperature_in, temperature_out, humidy, co2, o3);
        }


        public int 室外温度探头()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("室外温度探头");
            return res;
        }
        public int 二氧化碳探头()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("二氧化碳探头");
            return res;
        }
        public int 平均温度()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("平均温度");
            return res;
        }


        public int 大风机1组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("大风机1组开关");
            return res;
        }

        public int 大风机2组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("大风机2组开关");
            return res;
        }

        public int 大风机3组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("大风机3组开关");
            return res;
        }

        public int 大风机4组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("大风机4组开关");
            return res;
        }

        public int 侧排风1组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("侧排风1组开关");
            return res;
        }

        public int 侧排风2组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("侧排风2组开关");
            return res;
        }

        public int 侧排风3组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("侧排风3组开关");
            return res;
        }

        public int 侧排风4组开关()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("侧排风4组开关");
            return res;
        }

        public int 新风风机1()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("新风风机1");
            return res;
        }

        public int 新风风机2()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("新风风机2");
            return res;
        }

        public int 电动百叶窗开启()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("电动百叶窗开启");
            return res;
        }

        public int 电动百叶窗关闭()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("电动百叶窗关闭");
            return res;
        }

        public int 水泵()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("水泵");
            return res;
        }

        public int 导流板开启()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("导流板开启");
            return res;
        }

        public int 导流板关闭()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("导流板关闭");
            return res;
        }

        public int 加热器()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("加热器");
            return res;
        }

        public int 百叶窗开限位()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("百叶窗开限位");
            return res;
        }

        public int 百叶窗关限位()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("百叶窗关限位");
            return res;
        }

        public int 导流板开限位()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("导流板开限位");
            return res;
        }

        public int 导流板关限位()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("导流板关限位");
            return res;
        }

        public int PC与PLC切换()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("PC与PLC切换");
            return res;
        }

        public int 模式控制()
        {
            var plc = GetPlc();
            var res = plc.ReadInt32("模式控制");
            return res;
        }




        public HuanKongModel GetHuanKong()
        {

            var res = new HuanKongModel();
            var plc = GetPlc();
            try
            {
                var t_0 = plc.ReadInt32("水泵");
                res.ShuiLian_OK++;
            }
            catch (Exception ex)
            {
                res.ShuiLian_Error++;
                AppRuntime.Logger_Error(ex);
            }
            try
            {
                var t_0 = plc.ReadInt32("导流板开启");
                res.DaoLiuBan_OK++;
            }
            catch (Exception ex)
            {
                res.DaoLiuBan_Error++;
                AppRuntime.Logger_Error(ex);
            }
            try
            {
                var t_0 = plc.ReadInt32("电动百叶窗开启");
                res.BaiYeChuang_OK++;
            }
            catch (Exception ex)
            {
                res.BaiYeChuang_Error++;
                AppRuntime.Logger_Error(ex);
            }

            for(int i = 1; i <= 2; i++)
            {
                try
                {
                    var t_0 = plc.ReadInt32($"新风风机{i}");
                    res.XinFeng_OK++;
                }
                catch (Exception ex)
                {
                    res.XinFeng_Error++;
                    AppRuntime.Logger_Error(ex);
                }
            }
            
           

            for (int i = 1; i <=4 ; i++)
            {
              
                try
                {
                    var t_0 = plc.ReadInt32($"大风机{i}组开关");
                    res.DaFengJiZu_OK++;
                }
                catch (Exception ex)
                {
                    res.DaFengJiZu_Error++;
                    AppRuntime.Logger_Error(ex);
                }
            }

            for (int i = 1; i <=4; i++)
            {
               
                try
                {
                    var t_0 = plc.ReadInt32($"侧排风{i}组开关");
                    res.CePaiFeng_OK++;
                }
                catch (Exception ex)
                {
                    res.CePaiFeng_Error++;
                    AppRuntime.Logger_Error(ex);
                }
            }


            return res;
        }

        #region  modbus写入操作
        public void 模式控制(int model)
        {
            var plc = GetPlc();
            plc.Write("模式控制", model);
            AppRuntime.Logger_Info("设置模式控制");
        }
        public void 大风机1组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("大风机1组开关", value);
            AppRuntime.Logger_Info("大风机1组开关");
        }
        public void 大风机2组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("大风机2组开关", value);
            AppRuntime.Logger_Info("大风机2组开关");
        }
        public void 大风机3组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("大风机3组开关", value);
            AppRuntime.Logger_Info("大风机3组开关");
        }
        public void 大风机4组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("大风机4组开关", value);
            AppRuntime.Logger_Info("大风机4组开关");
        }
        public void 侧排风1组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("侧排风1组开关", value);
            AppRuntime.Logger_Info("侧排风1组开关");
        }
        public void 侧排风2组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("侧排风2组开关", value);
            AppRuntime.Logger_Info("侧排风2组开关");
        }
        public void 侧排风3组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("侧排风3组开关", value);
            AppRuntime.Logger_Info("侧排风3组开关");
        }
        public void 侧排风4组开关(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("侧排风4组开关", value);
            AppRuntime.Logger_Info("侧排风4组开关");
        }
        public void 新风风机1(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("新风风机1", value);
            AppRuntime.Logger_Info("新风风机1");
        }
        public void 新风风机2(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("新风风机2", value);
            AppRuntime.Logger_Info("新风风机2");
        }
        public void 电动百叶窗开启(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("电动百叶窗开启", value);
            AppRuntime.Logger_Info("电动百叶窗开启");
        }
        public void 电动百叶窗关闭(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("电动百叶窗关闭", value);
            AppRuntime.Logger_Info("电动百叶窗关闭");
        }
        public void 水泵(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("水泵", value);
            AppRuntime.Logger_Info("水泵");
        }
        public void 导流板开启(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("导流板开启", value);
            AppRuntime.Logger_Info("导流板开启");
        }
        public void 导流板关闭(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("导流板关闭", value);
            AppRuntime.Logger_Info("导流板关闭");
        }
        public void 加热器(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("加热器", value);
            AppRuntime.Logger_Info("加热器");
        }
        public void PC与PLC切换(bool open)
        {
            var plc = GetPlc();
            var value = open ? 1 : 0;
            plc.Write("PC与PLC切换", value);
            AppRuntime.Logger_Info("PC与PLC切换");
        }

        public void UpdateDevicePage(DosingEquipment info)
        {
            var plc = GetPlc();
            try
            {
                var dafengjizu = new int[]
                {
                    info.DaFengJi_open_1?1:0,
                    info.DaFengJi_open_2?1:0,
                    info.DaFengJi_open_3?1:0,
                    info.DaFengJi_open_4?1:0,
                    info.DaFengJi_open_5?1:0,
                };
                var cepaifeng = new int[]
                {
                    info.CePaiFeng_open_1?1:0,
                    info.CePaiFeng_open_2?1:0,
                    info.CePaiFeng_open_3?1:0,
                    info.CePaiFeng_open_4?1:0,
                    info.CePaiFeng_open_5?1:0,
                    info.CePaiFeng_open_6?1:0,

                };
                for(int i = 0; i < 4; i++)
                {
                    var value = dafengjizu[ i];
                    plc.Write($"大风机{i+1}组开关", value);
                }
                for (int i = 0; i < 4; i++)
                {
                    var value = cepaifeng[i];
                    plc.Write($"侧排风{i+1}组开关", value);

                }

                plc.Write("新风风机1", info.XinFengJi_open_1 ? 1 : 0);
                plc.Write("新风风机2", info.XinFengJi_open_2 ? 1 : 0);
                plc.Write("电动百叶窗开启", info.BaiYeChuang_open_1 );
                plc.Write("电动百叶窗关闭", info.BaiYeChuang_open_1==0?1:0);
                plc.Write("水泵", info.ShuiBeng_open_1);
                plc.Write("导流板开启", info.DaoLiuBan_open_1);
                plc.Write("导流板关闭", info.DaoLiuBan_open_1==0?1:0);
                plc.Write("加热器", info.JiaReQi_open_1);
            

            }
            catch (Exception ex)
            {
                AppRuntime.Logger_Error(ex);
            }
        }








        #endregion
    }
}
